package me.flyray.merchant;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import me.flyray.merchant.serverDemo.severImpl.SeverImpl;

public class MainActivity extends AppCompatActivity {

    private FrameLayout mFrameLayout;
    private TextView mA;
    private TextView mB;

    private SeverImpl sever;
    private Handler mHandler;
    private String response;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mFrameLayout = (FrameLayout) findViewById(R.id.frame);
        mA = (TextView) findViewById(R.id.a);
        mB = (TextView) findViewById(R.id.b);
        mA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeUI("0");
            }
        });
        mB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeUI("1");
            }
        });
        init();
        login();

        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {

                // 根据异步消息回调结果  改变当前UI线程
                switch (message.arg1) {
                    case 100:

                        // 组件之间通信工具
                        Activity_A fragment1 = new Activity_A();
                        Bundle bundle = new Bundle();
                        bundle.putString("response", response);
                        fragment1.setArguments(bundle);
                        //如果transaction  commit（）过  那么我们要重新得到transaction

                        FragmentManager fragmentManager = getSupportFragmentManager();
                        FragmentTransaction ft = fragmentManager.beginTransaction();
                        Fragment fA = new Activity_A();
                        Fragment fB = new Activity_B();
                        ft.replace(R.id.frame, fragment1);
                        ft.commit();
                        break;
                }

                return false;
            }
        });

    }


    public void login() {
        // 传入的是 当前activity实例    service层可以根据当前实例获取例如网络状态
        new Thread(new Runnable() {
            @Override
            public void run() {
                sever = new SeverImpl(MainActivity.this);
                response = sever.login("44444", "55555");

                // 通知UI线程  数据改变
                Message message = new Message();
                message.arg1 = 100;
                mHandler.sendMessage(message);

            }
        }).start();

    }


    public void changeUI(String flag) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        Fragment fA = new Activity_A();
        Fragment fB = new Activity_B();
        if ("0".equals(flag)) {
            ft.replace(R.id.frame, fA);
        }
        if ("1".equals(flag)) {
            ft.replace(R.id.frame, fB);
        }
        ft.commit();
    }

    private void init() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        Fragment fA = new Activity_A();
        ft.add(R.id.frame, fA);
        ft.commit();
    }


    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }



}
